<template>
  <div>
    <el-table :data="tableData" style="width: 100%; ">
      <el-table-column prop="name" label="分类名" align="left">
      </el-table-column>
      <el-table-column fixed="right" width="150">
        <!-- eslint-disable-next-line -->
        <template slot="header" slot-scope="scope">
          <el-button
            @click="showAddForm"
            type="text"
            size="medium"
            v-if="!flagChild"
            >添加或修改分类</el-button
          >
          <el-button
            @click="showBack"
            type="text"
            size="medium"
            v-else
            icon="back"
            >返回</el-button
          >
        </template>
        <template slot-scope="scope">
          <el-button @click="edit(scope.row)" type="text" size="small"
            >修改分类</el-button
          >
          <el-button
            @click="searchChild(scope.row)"
            type="text"
            size="small"
            v-if="!flagChild"
            >查看子分类</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <AddFormCategory
      ref="addForm"
      :tableData="tableData"
      :handleRefresh="handleRefresh"
      :itemData="itemData"
      @hide-add-form-category="handleHideAddFormCategory"
    />
  </div>
</template>
<script lang="ts">
import { Vue, Component, Model, Ref } from "vue-property-decorator";
import { MessageBox } from "element-ui";
import { getCategorys } from "@/apis/Category";
import { getReqDataNew } from "@/utils/http";
import { successDataType, failDataType, categoryType } from "CustomTypes";
import AddFormCategory from "@/components/AddFormCategory.vue";
import { isEqual } from "lodash";

@Component({
  name: "Category",
  components: { AddFormCategory }
})
export default class Category extends Vue {
  private tableData: Array<categoryType> = [];
  private itemData: categoryType = { _id: "", name: "", parentId: "" };
  @Ref("addForm")
  private readonly addForm!: AddFormCategory;
  private flagChild = false;

  edit(row: categoryType) {
    this.itemData = { ...row };
    this.addForm.dialogFormVisible = true;
  }

  searchChild(row: categoryType) {
    // console.log("search ", row);
    this.init(row._id);
  }

  async init(parentId = "0") {
    const result = await getCategorys(parentId);
    const { flag, data } = getReqDataNew(result);

    if (flag) {
      this.tableData = [...(data as successDataType).data];
      if (!isEqual("0", parentId)) this.flagChild = true;
      return;
    }

    MessageBox.alert((data as failDataType).msg, {
      confirmButtonText: "确定",
      type: "error"
    });
  }

  showAddForm() {
    this.addForm.dialogFormVisible = true;
  }

  showBack() {
    this.flagChild = false;
    this.init();
  }

  handleRefresh() {
    // console.log("父类 refresh");
    this.itemData = { _id: "", name: "", parentId: "" };
    this.init();
  }

  handleHideAddFormCategory() {
    this.itemData = { _id: "", name: "", parentId: "" };
  }

  mounted() {
    this.init();
  }
}
</script>
<style>
.el-table__header tr,
.el-table__header th {
  padding: 0;
  height: 50px;
  line-height: 50px;
  text-align: left;
}
/* 
.el-table__body th:nth-child(2) {
  text-align: left;
} */
</style>
